// 引入模块
const express = require('express');
// 开启路由
const r = express.Router();
//  链接数据库
const pool = require('../pool.js');

// 测试链接
r.get('/cs',(req,res)=>{
	res.send('order服务器成功开启')
});

// 添加订单列表
r.post("/v1/orderAdd",(req,res)=>{
	var obj = req.body;
	sql = 'insert into xz_order set ?'
	pool.query(sql,[obj],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows===0){
			res.send("0");
		}else{
			res.send("1");
		}
	});
});

// 删除订单列表
r.delete("/v1/orderDel/:aid",(req,res)=>{
	var $aid = req.params.aid;
	
	sql = "delete from xz_order where aid=?"
	pool.query(sql,[$aid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows===0){
			res.send("0");
		}else{
			res.send("1");
		}
	});
});

// 查询订单列表分页
r.get("/v1/orderList/:page&:count",(req,res)=>{
	var $page = req.params.page;
	var $count = req.params.count;
	$page = parseInt(($page));
	$count = parseInt(($count));
	
	sql = "select * from xz_order limit ?,?"
	pool.query(sql,[$page,$count],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send(result);
		}else{
			res.send("0");
		}
	});
});

// 查询订单列表根据aid
r.get("/v1/orderSearch/:aid",(req,res)=>{
	var $aid = req.params.aid;

	sql = "select * from xz_order where aid=?"
	pool.query(sql,[$aid],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send(result);
		}else{
			res.send("0");
		}
	});
});

// 插入订单详情
r.post("/v1/orderAdd",(req,res)=>{
	var obj = req.body;
	sql = 'insert into xz_order_detail set ?'
	pool.query(sql,[obj],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows===0){
			res.send("0");
		}else{
			res.send("1");
		}
	});
});

// 删除订单详情
r.delete("/v1/orderDel/:did",(req,res)=>{
	var $did = req.params.did;
	
	sql = "delete from xz_order_detail where did=?"
	pool.query(sql,[$did],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows===0){
			res.send("0");
		}else{
			res.send("1");
		}
	});
});

// 查询订单详情
r.get("/v1/orderSearch/:order_id",(req,res)=>{
	var $order_id = req.params.order_id;

	sql = "select * from xz_order_detail where order_id=?"
	pool.query(sql,[$order_id],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send(result);
		}else{
			res.send("0");
		}
	});
});








// 文件最后导出路由
module.exports = r;
